# coding = utf-8

import uvicorn as uvicorn
from fastapi import FastAPI, Query
from enum import Enum
from typing import Optional


class ModelGoodsType(str, Enum):
    renhe = '仁和'
    haoli = '好礼好健康'
    other = '其它'


# class ModelName(str, Enum):
#     alexnet = 'alexnet'
#     resnet = 'resnet'
#     lenet = 'lenet'

app = FastAPI()


# @app.get('/')
# def fastDemo():
#     return {'code': 200, 'content': '你是青春无敌美少女~~~'}
#
# @app.get('/api/{number}')
#
# async def read_number(number: int):
#     return {'数字': number}
#
# @app.get('/api/models/{model_name}')
# async  def get_model(model_name: ModelName):
#     if model_name == ModelName.alexnet:
#         return {'model_name': model_name, 'message': 'Deep Learning FTW'}
#     if model_name.value == ModelName.lenet:
#         return {'model_name': model_name, 'message': 'LeCNN all the images'}
#
# @app.get('/files/{file_path: path}')
# async def read_file(file_path: str):
#     return {'file_path': file_path}
#
# fake_items_db = [{'item_name': 'foo'}, {'item_name': 'bar'}, {'item_name': 'baz'}]

# @app.get('/items/')
# async def read_item(skip: int=0, limit: int=0):
#     return fake_items_db[skip+limit]

# @app.get('/items01/{item_id}')
# async def read_item01(item_id: str, q: Optional[str] = None):
#     if q:
#         return {'item_id': item_id, 'q': q}
#     return {'item_id': item_id}
#
# @app.get('/items02/{item_id}')
# async def read_item02(item_id: str, q: Optional[str] = Query(None, min_length=3, max_length=50, regex='^fixedquery$'), short: bool = False):
#     item = {'item_id': item_id}
#     if q:
#         item.update({'q': q})
#     if not short:
#         item.update(
#             {'descreption': 'This is an amazing item that has a long description'}
#         )
#     return item
#
# @app.get('/items03/{item_id}')
# async def read_item03(item_id: str, needy: str, skip: int = 0, limit: Optional[int] = Query('defaultValue')):
#     item = {'item_id': item_id, 'needy': needy, 'skip': skip, 'limit': limit}
#     return item


@app.get('/goodsType')
async def judge_goodsType(goodsType: str, cutPrice: float, costPrice: float, topPrice: float,
                          isrival: Optional[bool] = None):
    # 毛利率
    profitRate = (cutPrice - costPrice) / cutPrice
    # 折扣力度
    discount = cutPrice / topPrice


    if 0 < discount < 0.3:
        print('我是爆品')
        activtyGoods = '我是爆品'
        # return {'code': 0, 'msg': '我是爆品'}
    elif 0.3 <= discount < 0.98:
        print('我是折扣商品')
        activtyGoods = '我是爆品'
        # return {'code': 0, 'msg': '我是折扣商品'}

    if goodsType == '好礼好健康':
        if profitRate >= 0.35:
            # print('系统自动通过,执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount))

            return {'code': 0, 'msg': '我是%s系统自动通过,执行直降价,毛利率：%s，折扣系数：%s' % (activtyGoods, profitRate, discount)}
        elif 0 <= profitRate < 0.35:
            return {'code': 0, 'msg': '我是%s人工二级审批,执行直降价' % activtyGoods, 'profitRate': profitRate}
            # print('人工二级审批,执行直降价', profitRate)

        elif profitRate < 0:
            return {'code': 0, 'msg': '我是%s人工一级审批,执行直降价' % activtyGoods, 'profitRate': profitRate}
            # print('人工一级审批,执行直降价', profitRate)

    # 有竞对未二级审批，无竞对为一级审批
    if goodsType == '仁和':
        if discount > 0.8:
            return {'code': 0, 'msg': '我是%s系统自动通过，执行直降价,折扣系数' % activtyGoods, 'discount': discount}
            # print('系统自动通过，执行直降价,折扣系数：%s' % discount)
        elif discount <= 0.8:
            if isrival == 1:
                return {'code': 0, 'msg': '我是%s人工有竞对二级审批，执行直降价,折扣系数' % activtyGoods, 'discount': discount}
                # print('人工有竞对二级审批，执行直降价,折扣系数：%s' % discount)
            else:
                return {'code': 0, 'msg': '我是%s人工无竞对一级审批，执行直降价,折扣系数' % activtyGoods, 'discount': discount}
                # print('人工无竞对一级审批，执行直降价,折扣系数：%s' % discount)

    if goodsType == '其它':
        if profitRate >= 0.2:
            return {'code': 0, 'msg': '我是%s系统自动通过执行直降价,毛利率：%s，折扣系数：%s' % (activtyGoods, profitRate, discount)}
            # print('系统自动通过执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount))
        elif 0 <= profitRate < 0.2:
            return {'code': 0, 'msg': '我是%s人工二级审批，执行直降价,毛利率：%s，折扣系数：%s' % (activtyGoods, profitRate, discount)}
            # print('人工二级审批，执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount))
        elif profitRate < 0:
            return {'code': 0, 'msg': '我是%s人工一级审批,执行直降价,毛利率：%s，折扣系数：%s' % (activtyGoods, profitRate, discount)}
            # print('人工一级审批,执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount))
    return '请检查参数是否正确'

@app.get('/goodsTypeWithBi')
async def judge_goodsType(goodsType: str, skuid: str, name: str,
                          package_specifications: str, otc: str, cutPrice: float, costPrice: float, topPrice: float,
                          isrival: Optional[bool] = None):
    # 毛利率
    profitRate = (cutPrice - costPrice) / cutPrice
    # 折扣力度
    discount = cutPrice / topPrice
    # bi文件内容
    bi_O2O = '美团O2O####$$$$100012####$$$$叮当快药灯市口四十五店####$$$$' + skuid + '####$$$$' + name + '####$$$$' + package_specifications + '####$$$$'+ otc +'####$$$$' + str(
        cutPrice) + '####$$$$' + str(costPrice) + '####$$$$' + str(cutPrice) + '####$$$$德开医药（快速发全国）####$$$$' + str(
        topPrice)
    bi_B2C = '美团B2C####$$$$204752####$$$$北京仓####$$$$' + skuid + '####$$$$' + name + '####$$$$' + package_specifications + '####$$$$'+ otc +'####$$$$' + str(
        cutPrice) + '####$$$$' + str(costPrice) + '####$$$$' + str(cutPrice) + '####$$$$德开医药（快速发全国）####$$$$' + str(
        topPrice)
    if 0 < discount < 0.3:
        print('我是爆品')

        # return {'code': 0, 'msg': '我是爆品'}
    elif 0.3 <= discount < 0.98:
        print('我是折扣商品')
        # return {'code': 0, 'msg': '我是折扣商品'}

    if goodsType == '好礼好健康':
        if profitRate >= 0.35:
            # print('系统自动通过,执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount))

            return {'code': 0, 'msg': '系统自动通过,执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount), 'bi_O2O': bi_O2O,
                    'bi_B2C': bi_B2C}
        elif 0 <= profitRate < 0.35:
            return {'code': 0, 'msg': '人工二级审批,执行直降价', 'profitRate': profitRate, 'bi_O2O': bi_O2O, 'bi_B2C': bi_B2C}
            # print('人工二级审批,执行直降价', profitRate)

        elif profitRate < 0:
            return {'code': 0, 'msg': '人工一级审批,执行直降价', 'profitRate': profitRate, 'bi_O2O': bi_O2O, 'bi_B2C': bi_B2C}
            # print('人工一级审批,执行直降价', profitRate)

    # 有竞对未二级审批，无竞对为一级审批
    if goodsType == '仁和':
        if discount > 0.8:
            return {'code': 0, 'msg': '系统自动通过，执行直降价,折扣系数', 'discount': discount, 'bi_O2O': bi_O2O, 'bi_B2C': bi_B2C}
            # print('系统自动通过，执行直降价,折扣系数：%s' % discount)
        elif discount <= 0.8:
            if isrival == 1:
                return {'code': 0, 'msg': '人工有竞对二级审批，执行直降价,折扣系数', 'discount': discount, 'bi_O2O': bi_O2O,
                        'bi_B2C': bi_B2C}
                # print('人工有竞对二级审批，执行直降价,折扣系数：%s' % discount)
            else:
                return {'code': 0, 'msg': '人工无竞对一级审批，执行直降价,折扣系数', 'discount': discount, 'bi_O2O': bi_O2O,
                        'bi_B2C': bi_B2C}
                # print('人工无竞对一级审批，执行直降价,折扣系数：%s' % discount)

    if goodsType == '其它':
        if profitRate >= 0.2:
            return {'code': 0, 'msg': '系统自动通过执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount), 'bi_O2O': bi_O2O,
                    'bi_B2C': bi_B2C}
            # print('系统自动通过执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount))
        elif 0 <= profitRate < 0.2:
            return {'code': 0, 'msg': '人工二级审批，执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount), 'bi_O2O': bi_O2O,
                    'bi_B2C': bi_B2C}
            # print('人工二级审批，执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount))
        elif profitRate < 0:
            return {'code': 0, 'msg': '人工一级审批,执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount), 'bi_O2O': bi_O2O,
                    'bi_B2C': bi_B2C}
            # print('人工一级审批,执行直降价,毛利率：%s，折扣系数：%s' % (profitRate, discount))
    return '请检查参数是否正确'

# 本地访问 http://127.0.0.1:8081
# 外网访问需配置阿里云安全组(已配置)
# 公网ip+端口号，举例：http://39.102.92.227:8081
# 访问api文档：http://39.102.92.227:8081/docs
if __name__ == '__main__':
    # my_server为python文件名，server为前面实例化的服务名称
    uvicorn.run('api6:app', host="0.0.0.0", port=8081, debug=True)
